\\
\\ (PARI) R. J. Cano, Apr 25 2017
\\
\\ Title: "1, 2, 4, 4, 4, 8, 6, 4... Somewhere inside 2*A240009..."
\\
\\ ( Untitled: "Little but meaningful" things I owe to OEIS ) ...I believe to have found just few minutes ago the Answer!!! )
\\

/*

 Greetings.

These following lines are for sharing something to say about:

How-to: Shmuel Zaks discovered his prefix reversals algorithm illustrated by A055881?;

Nobody invented everything from the scratch, and each one of us must study and learn patiently and humbly what the human kind achieved in terms of pragmatic useful (and more important meaningful knowledge).

 For me it took 37 years, happiness, headaches, suffering, self-struggling battles, fun, joy, countless coffee cups, insomnia, e-v-e-r-y-t-h-i-n-g, a little bit of everything... 

I was deeply intrigued about such algorithm since I did read by first time its application illustrated by Mr. Joerg Arndt (and happily found the FXTBOOK which I today love)

( My precise question was: "Dude! (Remy), How a human did found out this! ???" )

This is what Zaks probably did:

-----------------------------------------------------------

He realized that if the output generated by the Heap's algorithm is "transformed into an uniformly complementary set", then the result will match 1.0e2 percent with the output generated by his prefix reversals algorithm, of course for the same input.

------------------------------------------------------------

Undeserved or not, I noticed it just less than an hour ago while mailing this.

But let us roll back in time to a little bit of history.

Karl F. Gauss did taught we humans trough a cool anecdote the formula for summation of the first naturals 1+2+3+4+5+...+n = n*(n+1)/2 = binomial(n+1, 2);

Well he realized that by reversing the sequence of numbers to sum, and by summing them as columns, the same result n+1 is repeated n times. It might be astonishing, to realize that the output for some ways of generating lists of permutations, behaves the same. I noticed it quite quite later...

Did mean that for example, you pick n=3 and ask someone else to generate permutations for 3 things listed with increasing order, he/she will reply you "trivial" giving quickly the list; ...If correct, and if  --in the sake of good taste and simplicity-- the chosen representation is the 3 digits for base 3 or trinary, i.e. [0,1,2], then the other 5 permutations are:

[0,2,1], [1,0,2], [1,2,0], [2,0,1], and [2,1,0];

The full list reversed now is:

[2,1,0], [2,0,1], [1,2,0], [1,0,2], [0,2,1], [0,1,2];

And by imitating Gauss, we get:

[0,1,2], [0,2,1], [1,0,2], [1,2,0], [2,0,1], [2,1,0];
[2,1,0], [2,0,1], [1,2,0], [1,0,2], [0,2,1], [0,1,2];
------------------------------------------------------------------
[2,2,2], [2,2,2], [2,2,2], [2,2,2], [2,2,2], [2,2,2];

Wow?... :-) I said that (but with a ! ) when at last tried it by fall 2015;

Among other implications, this result means that by symmetry considerations, we actually require to compute only n!/2 permutations. Notice also that those sums are represented by the greatest digit of base n (here n=3 for this present example), i.e. they are properly bounded... and bounded sets are desirable rather than anything else, specially for computing machinery.

Well, the same cannot be done (unfortunately) with the output generated neither by S-J-T nor Heap algorithms. Btw, it is not a signature of lexicographic generation of permutations. There exist yet at least another algorithm behaving in the same way. Yes!, it is not frequently mentioned, not so popular as Heap or S-J-T, but works fine and indeed faster. I name it by its author: Shmuel Zaks; An example of its application when n=4, was submitted to OEIS by Joerg Arndt in the seq. A055881 ( www.oeis.org/A055881 );

By imitating Gauss once more, but now this time for Zaks's algorithm output (when n=4):

[0,1,2,3], [1,0,2,3], [2,0,1,3], ..., [1,3,2,0], [2,3,1,0], [3,2,1,0]
[3,2,1,0], [2,3,1,0], [1,3,2,0], ..., [2,0,1,3], [1,0,2,3], [0,1,2,3]
----------------------------------------------------------------------------------------
[3,3,3,3], [3,3,3,3], [3,3,3,3], ..., [3,3,3,3], [3,3,3,3], [3,3,3,3]

There of course were removed from the middle 18 permutations and its corresponding reverses.

Re-thinking the things, we see that now we have two different algorithms behaving in such way. I called it "Uniformly complementary", defining it as follows:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Def. begins)
Any non-empty subset "A" of elements from a field, is "Uniformly complementary" if its elements satisfy that those pairs placed at symmetric positions have the same sum.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Def. ends)

Here "placed at symmetric positions" means positions that average the middle point (in the real numbers sense). For example if "A" has m elements, the middle point or edges average is (1+m)/2, and the only way of achieving it with a discrete variable (an integer counter), is to associate for any k such that 1<=k<=m, the pair of elements A[k] and A[1+m-k]; For such reason it is invoked there the notion of
symmetry.

The definition demands precisely the identity: A[k] + A[1+m-k] = C for a fixed C, and for every k in 1..m;

Specifically when applied to subsets of permutations, IF m=n!, and the least or more simple representation is used (digits for base n, or integers in 0..n-1) THEN C=n-1;

Very exciting I must say.

Now this answer storms a new enigma: "Given an uniformly complementary set: Does it exist some algorithm that reverses it back to the source from which it was (hypothetically talking), transformed into uniformly complementary ???".

Perhaps as long as we live, we will be "in requiem" due our habit of making questions.

Best regards,

Remy.

P.S.: a) This contains the following PARI/GP sourcecode for verifying among others these statements. b) Notice that if it happens between  Zaks's and Heap's algorithms, then there must exist (hypothetically talking) a Fifth permutation algorithm (currently unknown to me) besides 1) Narayana Pandit's, 2) Steinhaus-Johnson-Trotter, 3) Heap's, 4) Shmuel Zak's, ... yes a Fifth algorithm, such that it couples to (1) through the uniformly complementary transformation. (2) was an initial candidate, but it only works for n=2, so if exists, such Fifth algorithm is not the same than SJT's.


-----------------------------------=[ Sourcecode Starts ]=-----------------------------------*/

/* Implements the Zaks's algorithm */

Zaks_impl(n)={
  my(Q=n!,v=vector(n,j,j-1),t,k,m,w=vector(n!),u=(n-1)*vector(n,j,1));
  w[1]=v;
  w[#w]=u-v;  
  for(c=1,Q/2-1,
    m=c;
    k=2;
    while(!(m%k),m/=k;k++);
    for(i=1,k/2,
      t=v[i];
      v[i]=v[k-i+1];
      v[k-i+1]=t;
    );
    w[c+1]=v;
    w[Q-c]=u-v;
  );
  w
}

/* Implements the Heap's algorithm. */

heap_impl(n)={
  my(A=vector(n,j,j-1),Q=vector(n!,jj,vector(n)),c=vector(n),t,i,k=1);
  Q[1]+=A;
  while(i<n,
    if(c[i+1]<i,
      if(!(i%2),
        t      = A[1];
        A[1]   = A[i+1];
        A[i+1] =    t
      ,
        t           = A[c[i+1]+1];
        A[c[i+1]+1] = A[i+1];
        A[i+1]      = t
      );
      Q[k++]+=A;
      c[i+1]++;
      i=0
    ,
      c[i+1]=0;
      i++
    )
  );
  return(Q)
}

/*The following routine implements recursively the Steinhaus-Jhonson-Trotter algorithm trough a sieve based on modular arithmetic. Usage: Initial "d"iagonal: 0 "/" (default) or 1 "\"; "w"aving diagonals? 1 "yes" (default), or 0 "no"; Output "format" 1 "A vector of vectors" (default), or 0 "A huge vector" */

anSlowSJT_impl(n,d=0,w=1,format=1)={ /* Note: As it might be expected, the result is the n! permutations for all the digits (0..n-1) in base n */
  if(n<1,return([]));
  if(n==1,return([0]));
  if(n==2,if(d,if(format,return([[1,0],[0,1]]),return([1,0,0,1])),if(format,return([[0,1],[1,0]]),return([0,1,1,0]))));
  my(u=anSlowSJT_impl(n-1,d,w,0)); /* Internally we use only the output format zero. */
  my(c=1,sqr_n=n*n);
  d=!d;
  my(v=vector(n!*n,i,0*if(w&&!((i-1)%sqr_n),d=!d)+(n-1)*(!d*(((i%sqr_n)%(n-1)==1)&&(1!=(i%sqr_n)))+!!d*((!(i%sqr_n))||(1==((i%sqr_n)%n-(i%sqr_n)\n))))));
  for(k=0,(n-1)!-1,
    for(j=1,n,
      for(i=1,n-1,
        if(c<=#v,
          while(v[c],c++);
          v[c]=u[k*(n-1)+i];
          c++
        )
      )
    )
  );
  c*=0;
  if(format,return(vector(n!,y,vector(n,x,v[c++]))),return(v))
}

fixToUniformlyComplementary(v,n)={ \\ The concept applied specifically to permutation sets with n! different elements.
  my(w:list,s=(n-1)*vector(n,i,1),u=vector(#v,i,vector(n)),c=1,a);
  w=List();
  if((#v==n!)&&(n>1),
    for(k=1,#v,listput(w,v[k]));
    while(#w,
      u[c]=w[1];
      u[#u-c+1]=s-u[c];
      listpop(w,1);
      for(k=1,#w,if((s-u[c])==w[k],a=k;break));
      if(a,listpop(w,a));
      a*=0;
      c++
    );
    return(u)
  )
}

isUniformlyComplementary(S,n)={ \\ The concept applied specifically to permutation sets with n! different elements.
  my(v=(n-1)*vector(n,i,1));
  for(j=1,#S,
    if((S[j]+S[#S-j+1])!=v,return(0))
  );
  return(1);
}

\\ "H" Returns the anti-symmetric sequence obtained by subtracting the outputs among SJT's and Narayana Pandit's algorithms.
H(j,{d=0},{w=1})={fixToUniformlyComplementary(anSlowSJT_impl(j,d,w),j)-vector(j!,i,numtoperm(j,i-1)-vector(j,k,1))}

\\ "h" Counts how many corresponding elements were identical between the outputs used by "H".
h(j,{d=0},{w=1})={my(p=H(j,d,w),zero=vector(j));sum(k=1,#p,p[k]==zero)}

\\ Note: The extra parameters are taken into account for illustrating that the result depends on a particular setup of SJT execution.
\\ Tech. detail: Since everything inside is antisymmetric, vecsum won't work for distinguishing the zero vectors!

\\
\\ Example: Apparently, according to the OEIS table and by deleting the initial 1, such sample lies somewhere inside 2*A240009
\\
\\ (23:01) gp > vector(8,i,h(i))
\\ time = 3min, 23,686 ms.
\\ %2 = [1, 2, 4, 4, 4, 8, 6, 4]

/*-----------------------------------=[ Sourcecode ends ]=-----------------------------------*/

/*EOF*/